package example.simple_mindmap.common.designs.task;




import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

/**
 * @Program: 8.mindmap
 * @Author: 海里的鱼
 * @Create: 2024-08-26 09:22
 * @Name: 任务的抽象类类
 * @Desc: TODO
 */
public abstract class Task {

    //任务ID，唯一
    protected Long id;
    //任务名
    protected String name;
    //任务状态
    protected TaskEnum state = TaskEnum.UNEXECUTED;
    //开始时间
    protected LocalDateTime startTime;
    //结束时间
    protected LocalDateTime endTime;




    //自定义，任务执行函数
    public abstract boolean executeFunction() throws Exception;

    //执行任务，这个就是给上面的套一层捕获异常的逻辑，避免抛出异常导致程序停止
    public boolean execute(){
        //执行的初始时间
        this.startTime = LocalDateTime.now();
        //修改状态为"任务开始"
        this.state= TaskEnum.EXECUTING;
        //执行任务
        try {
            if(!this.executeFunction()){
                //执行的结束时间
                this.endTime= LocalDateTime.now();
                //修改状态为"执行失败"
                this.state = TaskEnum.FAILED;
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            //执行的结束时间
            this.endTime = LocalDateTime.now();
            //修改状态为"执行失败"
            this.state = TaskEnum.FAILED;
            return false;
        }
        //执行的结束时间
        this.endTime = LocalDateTime.now();
        //修改状态为"执行成功"
        this.state = TaskEnum.SUCCESS;
        return true;
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取运行时间
     * @Describe: TODO
     **/
     public Long getRunningTime(){
        if(startTime == null || endTime == null){
            return -1L;
        }
        return ChronoUnit.MILLIS.between(startTime, endTime);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取任务的信息
     * @Describe: TODO
     **/
     public String getTaskInfo(){
         StringBuilder info = new StringBuilder("{");
         info.append("\"name\":\"");
         info.append(name);
         info.append("\",\"state\":\"");
         info.append(state.getMessage());
         info.append("\",\"startTime\":\"");
         info.append(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
         info.append("\",\"endTime\":\"");
         info.append(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
         info.append("\",\"runningTime\":\"");
         info.append(ChronoUnit.MILLIS.between(startTime, endTime));
         info.append("\"}");
        return info.toString();
    }

    public TaskEnum getState() {
        return state;
    }

    public void setState(TaskEnum state) {
        this.state = state;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

